package com.linq.cool.file.controller;

import com.linq.cool.api.domain.SysFile;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.file.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: yqlin
 * @Date: 2020/12/31 9:49
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping
public class FileController {
    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;
    @Autowired
    private FileService fileService;

    @GetMapping("/demo")
    public Result<Object> demo() {
        return Result.ok(endpoint);
    }

    /**
     * 文件上传
     *
     * @param file 文件
     *
     * @return 文件基本信息
     */
    @PostMapping("/upload")
    public Result<SysFile> upload(@RequestParam("file") MultipartFile file) {
        try {
            String url = fileService.uploadFile(file);
            return Result.ok(new SysFile().setName(FilenameUtils.getBaseName(file.getOriginalFilename())).setUrl(url));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

}
